﻿import cgi
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.api import users
from Entities import *
#---------------------------
# Entities NganhNghe
#---------------------------
def Get(keyNguoiDung = None,NhaTuyenDungID=0):
    ntdNd_list = []
    results = NhaTuyenDung_NguoiDung.all()
    if keyNguoiDung !=None:
        try:
            nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
        except:
            return "Invalid Key"	
    results.filter('NguoiDung',nguoidung)
    if NhaTuyenDungID > 0:
        nhatuyendung = NhaTuyenDung.get_by_id(NhaTuyenDungID)
        results = results.filter("NhaTuyenDungID", nhatuyendung) 
    for ntdNd in results:
        ntdNd_list.append(ntdNd.NhaTuyenDungID.key())
    return ntdNd_list
    
def Add(keyNguoiDung,NhaTuyenDungID):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    nhatuyendung = NhaTuyenDung.get_by_id(NhaTuyenDungID)
    if nhatuyendung != None:                
        ntdNd = NhaTuyenDung_NguoiDung(NhaTuyenDungID=nhatuyendung,NguoiDung=nguoidung)
        ntdNd.put()         
        return "Complete"
    else:
        return "Error"

def Update(keyNguoiDung,NhaTuyenDungID):
    nganhnghe_update = NganhNghe.get_by_id(NganhNgheID)
    linhvuc = LinhVuc.get_by_id(LinhVucID)
    nganhnghe = db.GqlQuery("SELECT * FROM NganhNghe WHERE TenNganhNghe = :1 AND LinhVucID = :2 AND NguoiDung = :3",
                              TenNganhNghe, linhvuc, nguoidung)
    if linhvuc.count > 0 and nganhnghe_update.count > 0 : 
        if nganhnghe.count() == 0:
            nganhnghe_update.TenNganhNghe = TenNganhNghe
            nganhnghe_update.LinhVucID = linhvuc
            nganhnghe_update.put()
            return "Complete"
        else:
            return "Duplicate"
    else:
        return "Error"

        
def Delete(keyNguoiDung, NhaTuyenDungID = 0):
    try:
        nguoidung = NguoiDung.get(db.Key(keyNguoiDung)) 
    except:
        return "Invalid Key"
    results = NhaTuyenDung_NguoiDung.all().filter("NguoiDung", nguoidung)
    if (NhaTuyenDungID > 0):
        nhatuyendung = NhaTuyenDung.get_by_id(NhaTuyenDungID)
        results = results.filter("NhaTuyenDungID", nhatuyendung)
    for ntd in results:
        NhaTuyenDung_NguoiDung.delete(ntd)
    return "Complete"